var CcTableUtils = (function() {
	let TABLE_ID = '';
	let HIDDEN_COLUMN_INDEX_STRING = '';
	let FIELDS = '';
	let TABLE_DATA = [];
	let TABLE = null;
	let FIELD_ARRAY = [];

	function init(tableId,fields,hiddenColIndexString,tableData) {
		TABLE_ID = tableId;
		FIELDS = fields;
		HIDDEN_COLUMN_INDEX_STRING = hiddenColIndexString;
		TABLE_DATA = tableData;

		TABLE = document.getElementById(tableId);
		FIELD_ARRAY = fields.split(',');

		this.fillData();// 向表格填充数据
	}

	init.prototype = {
		fillData: function() {
			const that = this;

			if( !TABLE_DATA || TABLE_DATA.length <=0 ) return

			for(let i = 0;i<TABLE_DATA.length;i++){
				let tr = that.insertRowAtLast(TABLE);
				let currentColIndex = 0;

				for(let j = 0;j<FIELD_ARRAY.length;j++){
					const field = FIELD_ARRAY[j];

					currentColIndex = j;
					let td = that.insertCell(tr,currentColIndex);
					that.setCellVal(td,TABLE_DATA[i][field]);
				}

				let tdOperator = that.insertCell(tr,currentColIndex+1);
				that.setCellVal(tdOperator,`<a href='javascript:;' onclick='deleteRow(this)'>删除</a>`);

			}

			// 填充表格数据后隐藏指定列
			that.hideShowTableTd(TABLE_ID,HIDDEN_COLUMN_INDEX_STRING,false);
		},

		insertRowAtLast: function(tableRef){
			// 在表格最后追加一个新行
			let newRow = tableRef.insertRow(-1);
			return newRow;
		},
		insertCell:	function(tr,colIndex){
			let newTD = tr.insertCell(colIndex);
			return newTD;
		},
		setCellVal: function(td,val){
			td.innerHTML = val;
		},
		hideShowTableTd:function(tableId, columns, show) {
			var strs = new Array(); //定义一数组
			strs = columns.split(","); //字符分割
			var tableTd = "";
			for (var i = 0; i < strs.length; i++) {
			 tableTd += "td:eq(" + strs[i] + "),th:eq(" + strs[i] + "),"
			}
			tableTd = tableTd.substring(0, tableTd.length - 1);
			if (show) {
			 $('#' + tableId + ' tr').find(tableTd).show();
			}
			if (!show) {
			 $('#' + tableId + ' tr').find(tableTd).hide();
			}
 		}
	};
	
	return {
		init: init
	}
})();